package org.gaaidou.ptarmigan.springboot.core.common;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.util.NumberUtils;

import java.text.ParseException;
import java.util.*;

@Slf4j
public final class DateUtil extends DateUtils {

    private DateUtil() {
    }


    private static final Map<String, String[]> formatter = new LinkedHashMap<>();

    static {
        TimeZone.setDefault(TimeZone.getTimeZone(EnvUtil.getValue("TZ", "UTC")));

        List<String> types = List.of("-", "/", ".", "");
        List<String> formatList = List.of(
                "yyyy${split}MM${split}dd'T'HH:mm:ss.SSSXXX",
                "yyyy${split}MM${split}dd'T'HH:mm:ssXXX",
                "yyyy${split}MM${split}dd HH:mm:ss.SSS",
                "yyyy${split}MM${split}dd HH:mm:ss",
                "yyyy${split}MM${split}dd HH:mm",
                "yyyy${split}MM${split}dd HH",
                "yyyy${split}MM${split}dd",
                "yyyy${split}MM");
        types.forEach(type -> formatter.put(type, formatList.stream().map(v -> v.replace("${split}", type)).toArray(String[]::new)));

    }



    public static Date parse(String source) {
        Optional<Date> result = formatter.entrySet().stream()
                .filter(v -> source.contains(v.getKey()) && source.indexOf(v.getKey()) < 8 || ("".equals(v.getKey())))
                .map(v -> parse(source, v.getValue())).filter(Objects::nonNull)
                .findFirst();

        if (result.isPresent()) {
            return result.get();
        } else {
            if (FormatUtil.isNumeric(source)) {
                return new Date(NumberUtils.parseNumber(source, Long.class));
            } else {
                return null;
            }
        }
    }

    public static Date parse(String source, String... pattern) {
        try {
            return DateUtils.parseDate(source.trim(), pattern);
        } catch (ParseException e) {
            log.warn("Invalid date value: {}", e.getMessage(), e);
            return null;
        }
    }


    public static long diff(Date startDate, Date endDate, int type) {
        long diff = endDate.getTime() - startDate.getTime();
        switch (type) {
            case Calendar.SECOND -> {
                return diff / 1000;
            }
            case Calendar.MINUTE -> {
                return diff / 1000 / 60;
            }
            case Calendar.HOUR -> {
                return diff / 1000 / 60 / 60;
            }
            case Calendar.DATE -> {
                return diff / 1000 / 60 / 60 / 24;
            }
            case Calendar.MONTH -> {
                return diff / 1000 / 60 / 60 / 24 / 30;
            }
            case Calendar.YEAR -> {
                return diff / 1000 / 60 / 60 / 24 / 365;
            }
            default -> {
                return diff;
            }
        }
    }


    public static String format(Date date, String pattern) {
        return DateFormatUtils.format(date, pattern, TimeZone.getDefault());
    }


    public static String format(Date date) {
        return format(date, "yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
    }
}
